using System;
using System.Data.Entity;
using System.Reflection;
using System.Web.Http;
using System.Web.Mvc;
using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Integration.WebApi;
using yiCommerce.Core;
using yiCommerce.Core.Data;
using yiCommerce.Core.Infrastructure;
using yiCommerce.Core.Infrastructure.DependencyManagement;
using yiCommerce.Data;
using yiCommerce.Service.Loggings;
using yiCommerce.Service.Pictures;
using yiCommerce.Service.Products;
using yiCommerce.Service.Securitys;

namespace yiCommerce.Admin
{
    public class YiEngine : IEngine
    {


        //private ContainerManager _containerManager;


        //public ContainerManager ContainerManager
        //{
        //    get { return _containerManager; }
        //}

        private static IContainer container;

        public YiEngine()
        {
          

        }
     


        public void Initialize()
        {
            
            var builder = new ContainerBuilder();
            // Configure the container  


            //db
            builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerHttpRequest();
            builder.Register<DbContext>(c => new YiObjectContext()).InstancePerHttpRequest();


            //service
            builder.RegisterType<WebWorkContext>().As<IWorkContext>();
            builder.RegisterType<ProductService>().As<IProductService>();
            builder.RegisterType<ActivityService>().As<IActivityService>();
            builder.RegisterType<PermissionService>().As<IPermissionService>();
            builder.RegisterType<ProductSearchService>().As<IProductSearchService>();
            builder.RegisterType<PictureService>().As<IPictureService>();
            

            //controller
            var callingAssembly = Assembly.GetCallingAssembly();
            builder.RegisterControllers(callingAssembly);

            builder.RegisterApiControllers(callingAssembly);
            container = builder.Build();
            //var resolver = new AutofacWebApiDependencyResolver(container);

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

        }

        public void Initialize(IContainer container)
        {
            throw new NotImplementedException();
        }

        public T Resolve<T>() where T : class
        {
            try
            {
                return AutofacRequestLifetimeHttpModule.GetLifetimeScope(container, null).Resolve<T>();
            }
            catch (Exception e)
            {
                
            }
            return container.Resolve<T>();
        }

        public T ResolveNamed<T>(string name) where T : class
        {
            throw new NotImplementedException();
        }

        public object Resolve(Type type)
        {
            try
            {
                return AutofacRequestLifetimeHttpModule.GetLifetimeScope(container, null).Resolve(type);
            }
            catch (Exception e)
            {

            }

            return container.Resolve(type);
        }

       
    }
}